package conditions.operators

import conditions.BooleanNode
import values.Variable

class MatchNode extends BooleanNode {

   Variable v
   String regex
   
   
   public MatchNode() {}
   
   //public MatchNode(String val, String regex)
   public MatchNode(Variable v, String regex)
   {
      this.v = v
      this.regex = regex // TODO: ver que regex compila
   }
   
   @Override
   public boolean evaluate()
   {
      // Pido los valores resueltos      
      def val = v.value
      
      if (val instanceof String)
      {
         println "  " + val + " match " + regex
         return val.matches(regex)
      }
      
      //println val + " match " + regex
      //return val.matches(regex)
      throw new Exception('el valor debe ser String')
   }
   
   /**
   * Necesario para implementar el clone
   */
   boolean equals(Object other)
   {
      other instanceof EqualsNode && this.v.equals(other.v) && this.regex.equals(other.regex)
   }
}